/**
* 版权所有:版权所有(C) 2020，卓信科技
* 系统名称:FOL2020-财务在线
* 文件名称:UploadFile.java
* 模块名称:TODO(这里请补充模块名称)
* 创建日期:2020年5月19日
* 作        者:Baisong
* 内容摘要:TODO(文件上传)
*/
package com.zx.emanage.util.gen;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;

/**
 * 类  名  称:UploadFile
 *
 * 内容摘要:TODO(文件上传)
 *
 * 作        者:Baisong
 */
public class UploadFile
{

    public static int LEN;

    public static int BITRATE = 128;

    public static String SPRIT = "/";

    public static String MUSIC = "music";

    public static String MUSICFILES = "musicfiles";

    public static String USER = "user";

    /**
     * 读文件
     *
     * @param fileName
     * @return
     */
    public static ByteBuffer readFile(File f)
    {
        try
        {
            RandomAccessFile file = new RandomAccessFile(f, "rw");
            LEN = (int) file.length();
            FileChannel channel = file.getChannel();
            MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, LEN);

            return buffer.get(new byte[(int) file.length()]);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 写文件
     *
     * @param readFileName
     * @param writeFileName
     */
    @SuppressWarnings("resource")
    public static void writeFile(File readFile, String writeFileName)
    {
        try
        {
            String with = writeFileName.substring(writeFileName.length() - 3, writeFileName.length());
            if ("lrc".equalsIgnoreCase(with))
            {
                String fileEncode = EncodingDetect.getJavaEncode(readFile);
                if (!fileEncode.equalsIgnoreCase("UTF-8"))
                {
                    FileUtils.writeLines(new File(writeFileName), "UTF-8", FileUtils.readLines(readFile, fileEncode));
                    return;
                }
            }
            RandomAccessFile file = new RandomAccessFile(writeFileName, "rw");
            FileChannel channel = file.getChannel();
            ByteBuffer buffer = readFile(readFile);

            MappedByteBuffer bytebuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, LEN);
            for (int i = 0; i < LEN; i++)
            {
                bytebuffer.put(i, buffer.get(i));
            }
            bytebuffer.flip();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 解析LRC歌词文件
     * 
     * @param path
     *            lrc文件路径
     * @return
     */
    public static List<Map<String, String>> parse(String path)
    {
        // 存储所有歌词信息的容器
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        try
        {
            File file = new File(path);
            String fileEncode = EncodingDetect.getJavaEncode(path);
            // System.out.println(fileEncode+">>>>>");

            if (file.isFile() && file.exists())
            { // 判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), fileEncode);
                BufferedReader bufferedReader = new BufferedReader(read);
                String regex = "\\[(\\d{1,2}):(\\d{1,2}).(\\d{1,2})\\]"; // 正则表达式
                Pattern pattern = Pattern.compile(regex); // 创建 Pattern 对象
                String lineStr = null; // 每次读取一行字符串
                while ((lineStr = bufferedReader.readLine()) != null)
                {
                    // text = new String(text.getBytes("ISO-8859-1"), "UTF-8");
                    // lineStr = java.net.URLDecoder.decode(lineStr , "UTF-8");
                    Matcher matcher = pattern.matcher(lineStr);
                    while (matcher.find())
                    {
                        // 用于存储当前时间和文字信息的容器
                        Map<String, String> map = new HashMap<String, String>();
                        // System.out.println(m.group(0)); // 例：[02:34.94]
                        // [02:34.94] ----对应---> [分钟:秒.毫秒]
                        String min = matcher.group(1); // 分钟
                        String sec = matcher.group(2); // 秒
                        String mill = matcher.group(3); // 毫秒，注意这里其实还要乘以10
                        String str = "[" + min + ":" + sec + ":" + mill + "]";
                        // long time = getLongTime(min, sec, mill + "0");
                        // 获取当前时间的歌词信息
                        String text = lineStr.substring(matcher.end());
                        map.put(str, text); // 添加到容器中
                        list.add(map);
                    }
                }
                read.close();
                return list;
            }
            else
            {
                // System.out.println("找不到指定的文件:" + path);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 打印歌词信息
    */
    public static String printLrc(List<Map<String, String>> list)
    {
        String str = "";
        if (list == null || list.isEmpty())
        {
            // System.out.println("没有任何歌词信息！");
        }
        else
        {
            for (Map<String, String> map : list)
            {
                for (Entry<String, String> entry : map.entrySet())
                {
                    str += entry.getKey() + entry.getValue() + "\n";
                }
            }
        }
        // System.out.println(str);
        return str;
    }

    public static void main(String[] args) throws Exception
    {

    }
}
